package com.midea.leetcode;

import java.util.ArrayList;
import java.util.List;

public class LeetCode468 {
    public static void main(String[] args) {
        /*String s = "2001:0db8:85a3:0:0:8A2E:0370:7334:";
        String[] strs =  s.split(":");
        System.out.println(strs.length);
        for (String str : strs){
            System.out.println(str);
        }*/
        //System.out.println(validIPAddress("2001:0db8:85a3:0000:0:8A2E:0370:733a"));
        //System.out.println(validIPAddress("12..33.4"));
        System.out.println(validIPAddress("20EE:FGb8:85a3:0:0:8A2E:0370:7334"));
    }
    public static String validIPAddress(String IP) {
        if (IP.contains(".")){
            if (dfs4(IP)) return "IPv4";
            else return "Neither";
        }else if(IP.contains(":")){
            if (dfs6(IP)) return "IPv6";
            else return "Neither";
        }else {
            return "Neither";
        }
    }
    public static boolean dfs4(String IP){
        if (IP.endsWith(".")) return false;
        String[] strs =  IP.split("\\.");
        if (strs.length != 4 ) return false;
        for (String str : strs){
            if (str.length()==0)return false;
            if (str.length() == 0 || str.length() > 3 || !str.matches("[0-9]*") || Integer.parseInt(str) > 255 || (str.length() > 1 && str.startsWith("0"))){
                return false;
            }
        }
        return true;
    }
    public static boolean dfs6(String IP) {
        if (IP.endsWith(":")) return false;
        String[] strs =  IP.split(":");
        if (strs.length != 8) return false;
        String hexdigits = "0123456789abcdefABCDEF";
        for (String str : strs){
            if ( str.equals("") || str.length() > 4 ){
                return false;
            }
            for (Character c : str.toCharArray()){
                if (hexdigits.indexOf(c) == -1) return false;
            }
        }
        return true;
    }

    }
